package com.jhh.pay.center.actor;

import com.alibaba.fastjson.JSON;
import com.jhh.pay.center.api.data.NotifyRequest;
import com.jhh.pay.center.model.Order;
import com.jhh.pay.center.model.OrderState;
import com.jhh.pay.center.model.PayChannel;
import com.jhh.pay.center.model.TradeState;
import com.jhh.pay.center.search.DocTransformer;
import com.jhh.pay.center.service.OrderService;
import com.jhh.pay.center.service.PayChannelService;
import com.jhh.pay.center.service.cloud.PayChannelTemplate;
import com.jhh.pay.center.service.cloud.message.NotifyResolveBody;
import com.jhh.pay.center.service.cloud.message.TradeResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author tangxd
 * @Description: 回调ACTOR
 * @date 2017/12/14
 */
@Service("CallbackResolveActor")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Slf4j
public class CallbackResolveActor extends TradeActor {

    @Autowired
    private PayChannelService payChannelService;

    @Autowired
    private DocTransformer<TradeState, OrderState> stateTransformer;

    @Autowired
    private OrderService orderService;

    @Autowired
    private PayChannelTemplate template;

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(NotifyRequest.class, r -> {

                    PayChannel channel = payChannelService.getByKey(r.getChannelName());
                    if (channel == null) {
                        log.error("未找到对应的支付通道", r);
                        sender().tell(TradeResult.create(TradeState.ERROR, "SYS:NTF:0001", "未找到对应的支付通道"), self());
                        context().stop(self());
                        return;
                    }

                    NotifyResolveBody body = new NotifyResolveBody();
                    if (!setChannelCfg(body, channel)) {
                        return;
                    }
                    body.setBody(r.getBody());
                    setMessageExtension(body, channel, null);

                    try {
                        TradeResult mr = template.callbackResolve(channel, body);

                        Order order = orderService.get(mr.getSid());
                        order.setState(stateTransformer.transform(mr.getState()));
                        order.setLastResp(JSON.toJSONString(mr));
                        order.setLastRespTime(new Date());
                        if (mr.getState() == TradeState.SUCCESS) {
                            order.setSuccessTime(new Date());
                        }
                        orderService.update(order);
                        sender().tell(mr, self());
                    } catch (Exception e) {
                        e.printStackTrace();
                        log.error(e.getMessage(), e);
                        sender().tell(TradeResult.create(TradeState.ERROR, "SYS:DCH:0002", e.getMessage()), self());
                    }
                })
                .build();
    }
}
